package com.vshop.entity.interfacerequest;

import com.alibaba.fastjson.JSON;
import com.vshop.entity.callback.response.ResponseResultCallback;
import com.vshop.entity.interfacerequest.partnerlogin.response.ResponsePartnerLogin;
import com.vshop.entity.interfacerequest.partnerlogin.response.ResponsePartnerLoginData;
import com.vshop.entity.interfacerequest.usercardorder.response.ResponseUserCardOrder;
import com.vshop.entity.interfacerequest.usercardorder.response.ResponseUserCardOrderData;
import com.vshop.entity.interfacerequest.userinforeport.response.ResponseUserInfoReport;
import com.vshop.entity.interfacerequest.userinforeport.response.ResponseUserInfoReportData;
import com.vshop.entity.interfacerequest.userlogin.response.ResponseUserLogin;
import com.vshop.entity.interfacerequest.userlogin.response.ResponseUserLoginData;
import com.vshop.entity.interfacerequest.usernocardorder.response.ResponseUserNoCardOrder;
import com.vshop.entity.interfacerequest.usernocardorder.response.ResponseUserNoCardOrderData;
import com.vshop.entity.purchase.response.ResponsePurchaseService;
import com.vshop.utils.MD5;
import com.vshop.utils.Md5Encrypt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ExceptionRequest {
    /***
     * 计算前端上报的报文签名
     * @param str
     * @return
     */
    public String calculateSignature(String str) {
        String sign = Md5Encrypt.md5(str);
        return sign;
    }

    /**
     * 请求报文不合法（少参数或者不允许为空的参数值为空）
     *
     * @param connSeqNo
     * @param curtime
     * @return
     */
    public String dataWeiKong(String connSeqNo, String curtime) {
        ResponsePartnerLoginData data = new ResponsePartnerLoginData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeTime(curtime);
        data.setTradeRstCode("0002");
        data.setTradeRstMessage("请求报文不合法");
        // 6，给ResponsePackage赋值
        ResponsePartnerLogin resPackages = new ResponsePartnerLogin();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    /****
     * 非法登录，请确认账号或者密码是否正确
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @return
     */
    public String illegalLogin(String connSeqNo, String curtime, String tradeType) {
        ResponsePartnerLoginData data = new ResponsePartnerLoginData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("0003");
        data.setTradeRstMessage("非法登录：请确认账号或者密码是否正确!");

        ResponsePartnerLogin partnerLogin = new ResponsePartnerLogin();
        partnerLogin.setData(data);
        partnerLogin.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(partnerLogin);
        return jsonString;

    }

    /****
     * 非法登录，请确认账号或者密码是否正确
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @return
     */
    public String illegalLoginEN(String connSeqNo, String curtime, String tradeType) {
        ResponsePartnerLoginData data = new ResponsePartnerLoginData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("0003");
        data.setTradeRstMessage("Illegal login: please confirm whether the account or password is correct!");

        ResponsePartnerLogin partnerLogin = new ResponsePartnerLogin();
        partnerLogin.setData(data);
        partnerLogin.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(partnerLogin);
        return jsonString;
    }

    /****
     * 微信用户只上报了一个openid，没有其他的信息，也没有必要入库
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @param partnerCode
     * @param
     * @return
     */

    public String ncompleteReporting(String connSeqNo, String curtime, String tradeType, String partnerCode) {
        ResponseUserInfoReportData data = new ResponseUserInfoReportData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("0005");
        data.setTradeRstMessage("用户信息不全");
        ResponseUserInfoReport responseUserInfoReport = new ResponseUserInfoReport();
        responseUserInfoReport.setData(data);
        responseUserInfoReport.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

        String jsonString = JSON.toJSONString(responseUserInfoReport);
        return jsonString;

    }

    /****
     * 微信用户只上报了一个openid，没有其他的信息，也没有必要入库
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @param partnerCode
     * @param
     * @return
     */
    public String ncompleteReportingEN(String connSeqNo, String curtime, String tradeType, String partnerCode) {
        ResponseUserInfoReportData data = new ResponseUserInfoReportData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("0005");
        data.setTradeRstMessage("Incomplete User Information");
        ResponseUserInfoReport responseUserInfoReport = new ResponseUserInfoReport();
        responseUserInfoReport.setData(data);
        responseUserInfoReport.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

        String jsonString = JSON.toJSONString(responseUserInfoReport);
        return jsonString;
    }

    /***
     * 重复的下单请求
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @param partnerCode
     * @param
     * @param
     * @return
     */
    public String repeatRequest(String connSeqNo, String curtime, String tradeType, String partnerCode) {

        ResponseUserNoCardOrderData responseUserNoCardOrderData = new ResponseUserNoCardOrderData();
        responseUserNoCardOrderData.setConnSeqNo(connSeqNo);
        responseUserNoCardOrderData.setPartnerCode(partnerCode);
        responseUserNoCardOrderData.setTradeType(tradeType);
        responseUserNoCardOrderData.setTradeTime(curtime);
        responseUserNoCardOrderData.setTradeRstCode("0006");
        responseUserNoCardOrderData.setTradeRstMessage("重复的下单请求：orderNo重复！");

        ResponseUserNoCardOrder responseUserNoCardOrder = new ResponseUserNoCardOrder();
        responseUserNoCardOrder.setData(responseUserNoCardOrderData);
        responseUserNoCardOrder.setSign(Md5Encrypt.md5(JSON.toJSONString(responseUserNoCardOrder)));

        String jsonString = JSON.toJSONString(responseUserNoCardOrder);
        return jsonString;
    }

    /***
     * 重复的下单请求
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @param partnerCode
     * @param
     * @param
     * @return
     */
    public String repeatRequestEN(String connSeqNo, String curtime, String tradeType, String partnerCode) {
        ResponseUserNoCardOrderData responseUserNoCardOrderData = new ResponseUserNoCardOrderData();
        responseUserNoCardOrderData.setConnSeqNo(connSeqNo);
        responseUserNoCardOrderData.setPartnerCode(partnerCode);
        responseUserNoCardOrderData.setTradeType(tradeType);
        responseUserNoCardOrderData.setTradeTime(curtime);
        responseUserNoCardOrderData.setTradeRstCode("0006");
        responseUserNoCardOrderData.setTradeRstMessage("Repeat order request: orderNo repeat！");

        ResponseUserNoCardOrder responseUserNoCardOrder = new ResponseUserNoCardOrder();
        responseUserNoCardOrder.setData(responseUserNoCardOrderData);
        responseUserNoCardOrder.setSign(Md5Encrypt.md5(JSON.toJSONString(responseUserNoCardOrder)));

        String jsonString = JSON.toJSONString(responseUserNoCardOrder);
        return jsonString;
    }

    /***
     * 下订单北京接口异常返回的情况
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @param partnerCode
     * @param
     * @param retCode
     * @param resultInfo
     * @return
     */
    public String exceptionReturn(String connSeqNo, String curtime, String tradeType, String partnerCode, String retCode, String resultInfo) {

        ResponseUserCardOrderData data = new ResponseUserCardOrderData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode(retCode);
        if (retCode.equals("0010")) {
            data.setTradeRstMessage("卡号与渠道码不匹配，请选择有卡登录购买");
        } else {
            data.setTradeRstMessage(resultInfo);
        }
        ResponseUserCardOrder responseUserCardOrder = new ResponseUserCardOrder();
        responseUserCardOrder.setData(data);
        responseUserCardOrder.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

        String jsonString = JSON.toJSONString(responseUserCardOrder);

        return jsonString;
    }

    /***
     * 下订单北京接口异常返回的情况
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @param partnerCode
     * @param
     * @param retCode
     * @param resultInfo
     * @return
     */
    public String exceptionReturnEN(String connSeqNo, String curtime, String tradeType, String partnerCode, String retCode, String resultInfo) {

        ResponseUserCardOrderData data = new ResponseUserCardOrderData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode(retCode);
        if (retCode.equals("0009")) {
            data.setTradeRstMessage("No equipment!");
        } else if (retCode.equals("0010")) {
            data.setTradeRstMessage("The card number does not match the partnerCode,Please select a login card to purchase!");
        } else if (retCode.equals("0011")) {
            data.setTradeRstMessage("Query device real-time traffic usage information failed!");
        } else if (retCode.equals("0012")) {
            data.setTradeRstMessage("Monthly information is illegal, not empty and requires YYYYMM!");
        } else if (retCode.equals("0013")) {
            data.setTradeRstMessage("No monthly traffic usage record!");
        } else if (retCode.equals("0014")) {
            data.setTradeRstMessage("The start_day is illegal, can not be empty and requires YYYYMMDD!");
        } else if (retCode.equals("0015")) {
            data.setTradeRstMessage("The end_day is illegal, can not be empty and requires YYYYMMDD!");
        } else if (retCode.equals("0016")) {
            data.setTradeRstMessage("start_day is later than end_day!");
        } else if (retCode.equals("0017")) {
            data.setTradeRstMessage("end_day equals or is later than the current date!");
        } else if (retCode.equals("0018")) {
            data.setTradeRstMessage("Only allow to query daily traffic usage information for a maximum of 31 days!");
        } else if (retCode.equals("0019")) {
            data.setTradeRstMessage("The specified date is not valid, cannot be null and is required to be YYYYMMDD!");
        } else if (retCode.equals("0020")) {
            data.setTradeRstMessage("Failure downtime!");
        } else if (retCode.equals("0021")) {
            data.setTradeRstMessage("Reply the failure!");
        } else if (retCode.equals("0022")) {
            data.setTradeRstMessage("The current state of the device does not allow you to order a data plan!");
        } else if (retCode.equals("0023")) {
            data.setTradeRstMessage("This enterprise device is not allowed to order the data plan!");
        } else if (retCode.equals("0024")) {
            data.setTradeRstMessage("The ordering cycle is illegal and cannot be ordered!");
        } else if (retCode.equals("0025")) {
            data.setTradeRstMessage("Sign opening is not legal and cannot be ordered!");
        } else if (retCode.equals("0026")) {
            data.setTradeRstMessage("Failed to open the data plan!");
        } else if (retCode.equals("0027")) {
            data.setTradeRstMessage("This traffic plan order does not exist!");
        } else if (retCode.equals("0028")) {
            data.setTradeRstMessage("Only orders of [ordered] are allowed!");
        } else if (retCode.equals("0029")) {
            data.setTradeRstMessage("The order code cannot be empty!");
        } else if (retCode.equals("0030")) {
            data.setTradeRstMessage("Renewal of [in use] orders is allowed only!");
        } else if (retCode.equals("0031")) {
            data.setTradeRstMessage("The renewal period cannot be empty!");
        } else if (retCode.equals("0032")) {
            data.setTradeRstMessage("The renewal period is illegal and must be an integral multiple of the period unit ordered!");
        } else if (retCode.equals("0033")) {
            data.setTradeRstMessage("Only for [in use] orders are allowed to order fuel packs!");
        } else if (retCode.equals("0034")) {
            data.setTradeRstMessage("The refueling bag information is empty and cannot be ordered!");
        } else if (retCode.equals("0035")) {
            data.setTradeRstMessage("You cannot order this gas pack for this package!");
        } else if (retCode.equals("0036")) {
            data.setTradeRstMessage("It is only allowed to replace the refueling bag for [in use] orders!");
        } else if (retCode.equals("0037")) {
            data.setTradeRstMessage("No refueling bag has been ordered in this order, and the replacement operation is not allowed!");
        } else if (retCode.equals("0038")) {
            data.setTradeRstMessage("The filling bag information is empty and cannot be replaced!");
        } else if (retCode.equals("0039")) {
            data.setTradeRstMessage("This refueling bag cannot be replaced for this package!");
        } else if (retCode.equals("0040")) {
            data.setTradeRstMessage("The flow rate of the fuel pack is currently in use and cannot be replaced!");
        } else if (retCode.equals("0041")) {
            data.setTradeRstMessage("Cancellation of refueling bags is only allowed for [in use] orders!");
        } else if (retCode.equals("0042")) {
            data.setTradeRstMessage("The current flow of the fuel pack being used cannot be cancelled!");
        } else if (retCode.equals("0043")) {
            data.setTradeRstMessage("You cannot cancel the order without refueling bag!");
        } else if (retCode.equals("0044")) {
            data.setTradeRstMessage("The device_id field is not valid and cannot be null!");
        } else if (retCode.equals("0047")) {
            data.setTradeRstMessage("This enterprise is not allowed to renew the order, and the package will be discontinued!");
        } else {
            data.setTradeRstMessage("Interface results return exception information!");
        }
        ResponseUserCardOrder responseUserCardOrder = new ResponseUserCardOrder();
        responseUserCardOrder.setData(data);
        responseUserCardOrder.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

        String jsonString = JSON.toJSONString(responseUserCardOrder);

        return jsonString;
    }

    /***
     * 匹配企业码跟设备码是否一致
     * @param connSeqNo
     * @param curtime
     * @param tradeType
     * @param partnerCode
     * @return
     */
    public String unmatchPartnerCode(String connSeqNo, String curtime, String tradeType, String partnerCode) {

        ResponseUserInfoReportData data = new ResponseUserInfoReportData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("0008");
        data.setTradeRstMessage("当前设备码与上报的企业不匹配！");
        ResponseUserInfoReport responseUserInfoReport = new ResponseUserInfoReport();
        responseUserInfoReport.setData(data);
        responseUserInfoReport.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));

        String jsonString = JSON.toJSONString(responseUserInfoReport);

        return jsonString;

    }

    /***
     * 接口未成功返回数据的情况
     * @param connSeqNo
     * @param curtime
     * @return
     */
    public String unknownError(String connSeqNo, String curtime, String tradeType) {
        ResponseUserLoginData data = new ResponseUserLoginData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("9999");
        data.setTradeRstMessage("充值高峰，处理缓慢，请稍后重试!");

        ResponseUserLogin userLogin = new ResponseUserLogin();
        userLogin.setData(data);
        userLogin.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(userLogin);

        return jsonString;

    }


    public String unknownErrorEN(String connSeqNo, String curtime, String tradeType) {
        ResponseUserLoginData data = new ResponseUserLoginData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode("9999");
        data.setTradeRstMessage("processing is busy and slow, please try again later!");

        ResponseUserLogin userLogin = new ResponseUserLogin();
        userLogin.setData(data);
        userLogin.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(userLogin);

        return jsonString;
    }


    /***
     * 接口处理结果回调的response
     * @param payId
     * @param retCode
     * @param resultInfo
     * @return
     */
    public static String responseCallback(String payId, String retCode, String resultInfo) {

        ResponseResultCallback response = new ResponseResultCallback();
        response.setPayId(payId);
        response.setRetCode(retCode);
        response.setResultInfo(resultInfo);
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(response);
        log.info("【结果回调响应：】：" + resultInfo + ";响应：" + jsonString);
        return jsonString;
    }

    /***
     * 公共的返回方法
     * @param connSeqNo
     * @param partnerCode
     * @param curtime
     * @param tradeType
     * @param retCode
     * @param retMsg
     * @return
     */
    public String returnsPublic(String connSeqNo, String partnerCode, String curtime, String tradeType, String retCode, String retMsg) {

        ResponseUserLoginData data = new ResponseUserLoginData();
        data.setConnSeqNo(connSeqNo);
        data.setPartnerCode(partnerCode);
        data.setTradeType(tradeType);
        data.setTradeTime(curtime);
        data.setTradeRstCode(retCode);
        data.setTradeRstMessage(retMsg);

        ResponseUserLogin userLogin = new ResponseUserLogin();
        userLogin.setData(data);
        userLogin.setSign(Md5Encrypt.md5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(userLogin);
        return jsonString;
    }

    /**
     * 签名验证失败公用方法
     *
     * @param connSeqNo
     * @param tradeTime
     * @param tradeType
     * @param retCode
     * @param retMsg
     * @return
     */
    public String signatureFailure(String connSeqNo, String tradeTime, String tradeType, String retCode, String retMsg) {
        ResponsePartnerLoginData data = new ResponsePartnerLoginData();
        data.setConnSeqNo(connSeqNo);
        data.setTradeTime(tradeTime);
        data.setTradeType(tradeType);
        data.setTradeRstCode(retCode);
        data.setTradeRstMessage(retMsg);
        // 6，给ResponsePackage赋值
        ResponsePartnerLogin resPackages = new ResponsePartnerLogin();
        resPackages.setData(data);
        resPackages.setSign(MD5.calcMD5(JSON.toJSONString(data)));
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(resPackages);
        return jsonString;
    }

    /***
     * 规范返回
     * @param retCode
     * @param resultInfo
     * @return
     */
    public static String responseStr(String retCode, String resultInfo) {

        ResponsePurchaseService response = new ResponsePurchaseService();
        response.setRetCode(retCode);
        response.setResultInfo(resultInfo);
        // 返回给调用者的报文
        String jsonString = JSON.toJSONString(response);
        log.info("【规范的响应】：" + resultInfo + "响应：" + jsonString);
        return jsonString;

    }


}
